package com.vancouver.knows.ui.fragment.event;

import android.os.Bundle;
import android.os.Message;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.pangu.task.mark.ATaskMark;
import com.pangu.ui.browser.LoadableList;
import com.pangu.util.StringUtil;
import com.vancouver.knows.MainApplication;
import com.vancouver.knows.R;
import com.vancouver.knows.business.Constants;
import com.vancouver.knows.business.bean.UserInfo;
import com.vancouver.knows.business.task.mark.FetchEventsTaskMark;
import com.vancouver.knows.ui.base.BaseFragment;
import com.vancouver.knows.ui.base.LoadingFragment;
import com.vancouver.knows.util.AppUtils;

import butterknife.Bind;

/**
 * 活动子内容
 * 
 * @author chenxunlin01
 * 
 */
public class EventChildrenFragment extends BaseFragment {

	public static final String EVENT_TYPE_NEAR_FUTURE = "近期";
	public static final String EVENT_TYPE_MY = "我的";
	public static final String EVENT_TYPE_LEISURE = "休闲";
	public static final String EVENT_TYPE_SPORTS = "体育";
	public static final String EVENT_TYPE_FREE = "免费";
	public static final String EVENT_TYPE_ENTERTAINMENT = "文娱";
	public static final String EVENT_TYPE_COMMUNITY = "社区";
	public static final String EVENT_TYPE_FAMILY = "家庭";

	private EventPullControllerView eventListView;
	TextView firstLogin;

	public static EventChildrenFragment newInstance(String type) {
		EventChildrenFragment fragment = new EventChildrenFragment();
		Bundle bundle = new Bundle();
		bundle.putString("type", type);
		fragment.setArguments(bundle);

		return fragment;
	}

	@Override
	protected View createView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
		String type = getArguments().getString("type");
		if(!EVENT_TYPE_MY.equals(type)){
			eventListView = new EventPullControllerView(getActivity(),type);
			if (getParentFragment() instanceof LoadableList.LoadableListScrollListener) {
				eventListView.setScrollListener((LoadableList.LoadableListScrollListener) getParentFragment());
			}
			return eventListView;
		}else{
			ViewGroup view = (ViewGroup) inflater.inflate(R.layout.event_my,container,false);
			firstLogin = (TextView) view.findViewById(R.id.firstLogin);
			eventListView = new EventPullControllerView(getActivity(),type);
			if (getParentFragment() instanceof LoadableList.LoadableListScrollListener) {
				eventListView.setScrollListener((LoadableList.LoadableListScrollListener) getParentFragment());
			}
			view.addView(eventListView);

			return view;
		}
	}

	@Override
	public void onResume() {
		super.onResume();
		updatelayout();
	}

	private void updatelayout(){
		String type = getArguments().getString("type");
		if(EVENT_TYPE_MY.equals(type)){
			String userId = "";
			UserInfo userInfo = bModule.getCacheManager().getUserInfo();
			if (userInfo != null) {
				userId = userInfo.getUserID();
			}

			if(StringUtil.isEmptyString(userId)){
				eventListView.setVisibility(View.GONE);
				firstLogin.setVisibility(View.VISIBLE);
			}else{
				eventListView.setVisibility(View.VISIBLE);
				firstLogin.setVisibility(View.GONE);
			}
		}

		eventListView.flush();
	}

	public void subHandleMessage(Message msg) {
		switch (msg.what) {
		case Constants.BUSINESS_STATUS_USER_FOLLOW:
		case Constants.BUSINESS_STATUS_USER_DELETE_FOLLOW:
		case Constants.BUSINESS_STATUS_EVENT_LIST_CHANGE:
			updatelayout();
			break;
		}
	}

	@Override
	protected void bindView(View rootView) {
		eventListView.loadData();
	}

	public void scrollUp() {
		if (eventListView != null) {
			eventListView.scrollUp();
		}
	}

}
